package com.example.week9_advanceui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.Arrays;

public class ListView4_Activity extends AppCompatActivity {
    //要展示的文字
    private String[] texts=new String[]{"钟楼","桥","南门","东门","飞腾","三元湖"};
    //要展示的图片
    private Integer[] images=new Integer[]{R.mipmap.belltower,
            R.mipmap.bridge,
            R.mipmap.southgate,
            R.mipmap.eastgate,
            R.mipmap.flyball,
            R.mipmap.lake
    };
    //定义 ListView 组件
    ListView mListView = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview4);
    //设置 ListView 作为显示
        mListView = findViewById(R.id.listview4);
    //创建适配器，用的是自定义 TextImageAdapter，其父类是 BaseAdapter
        TextImageAdapter adapter = new TextImageAdapter(this, Arrays.asList(texts), Arrays.asList(images));
    //设置列表视图要用的适配器
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(ListView4_Activity.this,
                        "您选择了" + texts[position], Toast.LENGTH_LONG).show();
            }
        });
    }
}